package scene;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.Window;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLJPanel;
import javax.swing.JFrame;

    public class Fullscreen extends JFrame {

	/**
		 *
		 */
		private static final long serialVersionUID = -6047901210045772533L;
	private GraphicsDevice gDevice;
	private boolean fullscreen;

	private DisplayMode oldMode;
	public GLJPanel myglcanvas;

	public Fullscreen(GLJPanel glcanvas)
	{
		myglcanvas=glcanvas;
		add(glcanvas);
		set();
		System.out.println("lets go");
	}


	void set()
	{

            

		/**
		 * fen�tre que l'on d�sire passer en plein �cran
		 */
		//setMyFrame(_frame);
		//_frame.setBackground(new Color(0,255,0));
		/**
		 * on r�cup�re le p�riph�rique graphique
		 */
		setGDevice(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());



		/**
		 * on regarde si l'on n'est pas d�j� en mode plein �cran
		 * si oui true si non false
		 */
		setFullscreen((gDevice.getFullScreenWindow()!=null)?true:false);
		System.out.println("plein ecran : "+fullscreen);


		if(!isFullscreen())
		{


			/**
			 * on sauvegarde le mode d'affichage
			 */
			setOldMode(getGDevice().getDisplayMode());

	        /*
	         * Mettre en plein �cran
	         * Penser � rel�cher la fen�tre et �ter la d�coration
	         */
	        if(isDisplayable()) dispose();
	        setUndecorated(true);
	        setVisible(true);
	        getGDevice().setFullScreenWindow(this);


	        /*
	         * Search the display mode with the user settings
	         */
	        DisplayMode newDisplayMode = catchDisplayMode(
	        		getGDevice().getDisplayModes(),                   //all display mode available
	            Toolkit.getDefaultToolkit().getScreenSize().width,               //screen resolution
	            Toolkit.getDefaultToolkit().getScreenSize().height,
	            getOldMode().getBitDepth(),      //keep the current bit depth
	            getOldMode().getRefreshRate());  //keep the current refresh rate

	        /*
	         * Set the Display Mode found
	         */
	        getGDevice().setDisplayMode(newDisplayMode);



		}
		else
		{

			/**
			 * on quitte le mode plein �cran
			 */
            getGDevice().setFullScreenWindow(null);






		}




	}


	private boolean isFullscreen() {
		return fullscreen;
	}


	private void setFullscreen(boolean fullscreen) {
		this.fullscreen = fullscreen;
	}


	private GraphicsDevice getGDevice() {
		return gDevice;
	}


	private void setGDevice(GraphicsDevice device) {
		gDevice = device;
	}




	private DisplayMode getOldMode() {
		return oldMode;
	}


	private void setOldMode(DisplayMode _oldMode) {
		this.oldMode = _oldMode;
	}


    /**
     * Catch the display mode in the displayModes array corresponding to the parameter of the method
     *
     * If zero displayMode match with the parameter, we take the first display mode in the array
     *
     * @author Pepijn Van Eeckhoudt
     */
    private static DisplayMode catchDisplayMode(DisplayMode[] displayModes, int requestedWidth, int requestedHeight,
            int requestedDepth, int requestedRefreshRate)
    {
        //Try to find the exact match
        DisplayMode displayMode = searchDisplayMode(displayModes, requestedWidth, requestedHeight,
                                                    requestedDepth, requestedRefreshRate);

        //If the previous trial has failled, try again ignoring the requested bit depth and refresh rate
        if(displayMode == null)
        {
            displayMode = searchDisplayMode(displayModes, requestedWidth, requestedHeight,
                                            -1, -1);

            //Try again, and again ignoring the requested bit depth and height
            if(displayMode == null)
            {
                displayMode = searchDisplayMode(displayModes, requestedWidth,
                                                -1, -1, -1);

                //If all else fails try to get any display mode
                if(displayMode == null)
                    displayMode = searchDisplayMode(displayModes, -1,
                                                    -1, -1, -1);
            }
        }
        else
        {
            System.out.println("Perfect DisplayMode Found !!!");
        }

        return displayMode;
    }

    /**
     * Search the DisplayMode in the displayModes array corresponding to the parameter of the method
     *
     * @author Pepijn Van Eeckhoudt
     */
    private static DisplayMode searchDisplayMode(DisplayMode[] displayModes, int requestedWidth, int requestedHeight,
            int requestedDepth, int requestedRefreshRate)
    {
        //the display mode to use
        DisplayMode displayModeToUse = null;

        /*
         * Search the display mode to use in the displayMode array
         *
         * If this methods is called with -1 for all parameters, we take the first displayMode
         */
        for(int i = 0; i < displayModes.length; i++)
        {
            DisplayMode displayMode = displayModes[i];

            if((requestedWidth == -1 || displayMode.getWidth() == requestedWidth) &&
                (requestedHeight == -1 || displayMode.getHeight() == requestedHeight) &&
                (requestedHeight == -1 || displayMode.getRefreshRate() == requestedRefreshRate) &&
                (requestedDepth == -1 || displayMode.getBitDepth() == requestedDepth))
                    displayModeToUse = displayMode;
        }

        return displayModeToUse;
    }


    private static void switchToFullscreenMode(Frame glContainer, Toolkit settings)
    {
        /*
         * Get the default GraphicsDevice
         */
        GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();

        /*
         * Set fullscreen to the container
         */
        if(glContainer.isDisplayable()) glContainer.dispose();
        glContainer.setUndecorated(true);
        glContainer.setVisible(true);
        graphicsDevice.setFullScreenWindow(glContainer);


        /*
         * Search the display mode with the user settings
         */
        DisplayMode displayMode = catchDisplayMode(
            graphicsDevice.getDisplayModes(),                   //all display mode available
            settings.getScreenSize().width,               //screen resolution
            settings.getScreenSize().height,
            graphicsDevice.getDisplayMode().getBitDepth(),      //keep the current bit depth
            graphicsDevice.getDisplayMode().getRefreshRate());  //keep the current refresh rate

        /*
         * Set the Display Mode found
         */
        graphicsDevice.setDisplayMode(displayMode);
    }




    public void quit()
    {
		//this.setVisible(false);
		this.dispose();
    }



}
